package com.bg.business.controller;

import com.bg.business.feign.AccountFeign;
import com.bg.business.feign.OrderFeign;
import com.bg.business.feign.StockFeign;
import io.seata.spring.annotation.GlobalTransactional;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.math.BigDecimal;

@RestController
@RequestMapping("/business")
public class BusinessController {

    @Autowired
    private OrderFeign orderFeign;

    @Autowired
    private StockFeign stockFeign;

    @Autowired
    private AccountFeign accountFeign;

    @GlobalTransactional
    @RequestMapping("/toOrder")
    public void toOrder(String userId, String commodityCode, Integer count, BigDecimal amount) {
        accountFeign.reduce(userId, amount);
        stockFeign.deduct(commodityCode, count);
        orderFeign.add(userId, commodityCode, count, amount);
    }

}
